home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / GETCMT.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  10KB  |  268 lines

  1. /* getcmt.c - get comments from a C or C++ source file    */
  2. /*
  3.                                Byte_Magic Software
  4.                              9850 Meadowglen Ln. #35
  5.                                Houston, Tx.  77042
  6.                                  (713) 975-9033
  7.  
  8. Author:     Greg Messer
  9. Program:    getcmt.c
  10. Purpose:    Isolate comments from a C or C++ source file. Filter.
  11. Syntax:     getcmt [/L?] [filename]
  12.             (may use redirection for file and/or device I/O)
  13. Release:    Released to the Public Domain by Byte_Magic Software.
  14. Date:       07-11-88 GM...
  15.                 First release.
  16. Revised:    01-13-90 GM...
  17.                 Streamlined logic.
  18.             01-15-90 Bob Stout (RBS)...
  19.                 Fixed unsigned char error as return from getc().
  20.             01-22-90 GM...
  21.                 Fixed bug handling "xx/" (xx = **) at end of comment.
  22.                 Added C++ comment extraction.
  23.                 Added return of count to OS (ERRORLEVEL in MS-DOS).
  24.             01-24-90 RBS
  25.                 Added filename spec option
  26.                 Added /? switch
  27. System:     Compiled with Zortech C V2.06 under MS-DOS 3.20
  28.             for IBM PCs and compatibles.
  29. Rules:      ANSI C comments begin with /x and end with x/. (x = *).
  30.             Comments do not nest and do not appear in string or character
  31.             constants.
  32.             C++ comments begin with double slashes and end at EOL.
  33.             A Microsoft extension to C allows C++ style comments to serve as
  34.             single-line comments in C source.
  35. Comments:   Useful for creating documentation and improving commenting style.
  36.             Input may be from a specified filename or stdin.
  37.             Output is to stdout, so use DOS redirection for output.
  38.             Messages go to stderr so they are not redirectable from the screen.
  39.             Returns ERRORLEVEL = number of comments in source file(s).
  40. Examples:
  41.             Example... Output to screen:
  42.             getcmt < cfile.c
  43.                (displays comments from cfile.c on screen)
  44.             type cfile.c | getcmt
  45.                (same as above but slightly slower)
  46.             getcmt < cfile.c | more
  47.                (same as above, but pauses after each full screen)
  48.             getcmt cfile.c /l | more
  49.                (same as above, but display line numbers)
  50.  
  51.             Example... Output to printer:
  52.             getcmt < cfile.c > prn
  53.                (same as above but prints output on printer)
  54.             type cfile.c | getcmt > prn
  55.                (same as above but slightly slower)
  56.  
  57.             Example... Output to file:
  58.             getcmt < cfile.c > cfile.cmt
  59.                (writes cfile.c comments to cfile.cmt, overwriting existing file)
  60.             getcmt < cfile.c >> cfile.doc
  61.                (writes cfile.c comments to end of cfile.doc (appends))
  62.                        
  63.             getcmt /?
  64.                (displays help screen, returns ERRORLEVEL = 0)
  65.             getcmt /x
  66.                (invalid option - displays help screen, returns ERRORLEVEL = -1)
  67.  
  68.             For complete instructions on using redirection symbols, consult
  69.             the PC-DOS or MS-DOS manual or a general DOS reference book.
  70. */
  71.  
  72. #include <stdlib.h>
  73. #include <stdio.h>
  74. #include <ctype.h>
  75. #include <string.h>
  76.  
  77. #define LOOKS_GREAT 1
  78. #define LESS_FILLING 0
  79.  
  80. int extract_c_cmts(void);
  81. void inside_c_cmt(int);
  82.  
  83. FILE *infile = stdin;                   /* read input from here             */
  84. int  show_nos = 0;                      /* 0 = don't display line numbers   */
  85. int  line_no = 1;                       /* line_no = line number            */
  86.  
  87. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  88.  
  89. main(int argc, char *argv[])                            /* main logic:      */
  90. {
  91.     register int i;
  92.     const char *hype =
  93.         "\nGETCMT v1.1 - GET CoMmenTs\nby Byte_Magic Software\n";
  94.     const char *help =
  95.         "\nUsage: GETCMT [/l?] [filename | <sourcefile.ext] "
  96.         "[>destination file or device]\n"
  97.         "Options:  l - Print line numbers\n"
  98.         "          ? - Help\n"
  99.         "\nFilename optional - Reads source code from stdin "
  100.         "(Ctrl-C to quit before EOF)\n";
  101.     const char *oops =
  102.         "\a*** GETCMT - Can't open input file ";
  103.                                             /* display messages to operator */
  104. #if LOOKS_GREAT
  105.     fputs(hype, stderr);
  106. #elif LESS_FILLING
  107.     i = 0;
  108.     while(hype[i] != '\0')
  109.         putc(hype[i++], stderr);
  110. #endif
  111.  
  112.     if (1 < argc)
  113.     {
  114.         for (i = 1; i < argc; ++i)
  115.         {
  116.             if ('/' == *argv[i])
  117.             {
  118.                 if ('l' == tolower(argv[i][1]))
  119.                     show_nos = 1;
  120.                 else
  121.                 {
  122.                     int ercode;
  123.  
  124.                     ercode = ('?' == argv[i][1]) ? 0 : -1;
  125. #if LOOKS_GREAT
  126.                     fputs(help, stderr);
  127. #elif LESS_FILLING
  128.                     i = 0;
  129.                     while(help[i] != '\0')
  130.                         putc(help[i++], stderr);
  131. #endif
  132.                     if (ercode)             /* output BEL if invalid seitch */
  133.                         putc('\a', stderr);
  134.                     return(ercode);
  135.                 }
  136.             }
  137.             else
  138.             {
  139.                 infile = fopen(argv[i], "r");
  140.                 if (!infile)
  141.                 {
  142. #if LOOKS_GREAT
  143.                     fputs(oops, stderr);
  144.                     fputs(argv[i], stderr);
  145. #elif LESS_FILLING
  146.                     char *p = argv[i];
  147.  
  148.                     i = 0;
  149.                     while (oops[i])
  150.                         putc(oops[i], stderr);
  151.                     i = 0;
  152.                     while (*p)
  153.                         putc(*p++, stderr);
  154. #endif
  155.                 }
  156.             }
  157.         }
  158.     }
  159.  
  160.     i = extract_c_cmts();                   /* extract comments in infile   */
  161.     putc('\n', stdout);
  162.  
  163.     return(i);                              /* return number of comments to */
  164.                                             /* OS (ERRORLEVEL in DOS)       */
  165. }
  166.  
  167. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  168.  
  169. int extract_c_cmts()                        /* comment extraction logic:    */
  170. {
  171.     register int chi, cht;              /* chi = char in, cht = char test   */
  172.     int count;                          /* count = comment count            */
  173.  
  174.     count = 0;
  175.     chi = getc(infile);
  176.     while(chi != EOF)                       /* as long as there is input... */
  177.     {
  178.         if(chi == '/')                      /* process comments             */
  179.         {
  180.             cht = getc(infile);
  181.             if(cht == EOF)
  182.                 return(count);
  183.             if(cht == '*' || cht == '/')    /* if start of a comment...     */
  184.             {
  185.                 count++;                    /* count it and                 */
  186.                 inside_c_cmt(cht);          /* output all of the comment    */
  187.             }
  188.             else
  189.                 ungetc(cht, infile);
  190.         }
  191.         if ('\n' == chi)
  192.             line_no += 1;
  193.         chi = getc(infile);                  /* continue scanning input     */
  194.     }
  195.     return(count);
  196. }
  197.  
  198. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  199.  
  200. char *lntoaz(void)                      /* line number to zero-padded ASCII */
  201. {
  202.     int i, num = line_no;
  203.     static char numbuf[] = "0000: ";
  204.  
  205.     if (9999 < num)
  206.         strncpy(numbuf, "0000", 4);
  207.     else for (i = 3; i >= 0; --i)
  208.     {
  209.         numbuf[i] = (char)('0' + num % 10);
  210.         num /= 10;
  211.     }
  212.     return numbuf;
  213. }
  214.  
  215. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  216.  
  217. void inside_c_cmt(int ch)                   /* comment output logic:        */
  218. {                                           /* input ch = either '*' for C  */
  219.                                             /* or '/' for C++               */
  220.  
  221.     register int chi, cht;              /* chi = char in, cht = char test   */
  222. #if LESS_FILLING
  223.     char *p;
  224. #endif
  225.  
  226.     if(ch == '/')                           /* make ch = '\n' if C++        */
  227.         ch = '\n';                          /* note: ch is already 1st char */
  228.                                             /* of end comment if this is C  */
  229.     putc('\n', stdout);
  230.     if (show_nos)
  231.     {
  232. #if LOOKS_GREAT
  233.     fputs(lntoaz(), stdout);
  234. #elif LESS_FILLING
  235.     p = lntoaz();
  236.         while (*p)
  237.         putc(*p++, stdout);
  238. #endif
  239.     }
  240.     chi = getc(infile);
  241.     while(chi != EOF)                       /* as long as there is input... */
  242.     {                                       /* process comments             */
  243.         if(chi == ch)
  244.         {
  245.             if(ch == '\n')                  /* if C++ comment is ended...   */
  246.                 return;                     /* stop outputting              */
  247.             cht = getc(infile);
  248.             if(cht == '/')                  /* if C comment is ended...     */
  249.                 return;                     /* stop outputting              */
  250.             else
  251.             {
  252.                 ungetc(cht, infile);
  253.                 putc(chi, stdout);
  254.             }
  255.         }
  256.         else
  257.             putc(chi, stdout);              /* else comment text, output it */
  258.         if ('\n' == chi)
  259.             line_no += 1;
  260.                                             
  261.         chi = getc(infile);                  /* continue scanning input     */
  262.     }
  263.     return;
  264. }
  265.  
  266. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  267. /* end of getcmt.c  */
  268.